package com.xhsj.user.lsm.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhsj.user.lsm.entity.TbBadgesManager;
import com.xhsj.user.lsm.service.TbBadgesManagerService;
import com.xhsj.user.utils.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 证章管理 前端控制器
 * </p>
 *
 * @author suxiaolin
 * @since 2020-03-16
 */
@Slf4j
@Api(tags = "证章管理")
@RestController
@RequestMapping("/process/tbBadgesManager")
public class TbBadgesManagerController {

	@Autowired
    public TbBadgesManagerService service;


	@ApiOperation(value = "证章管理,保存/修改", notes = "证章管理,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    //@PreAuthorize("hasAuthority('tb:badges:manager:edit')")
	public Message save(@RequestBody TbBadgesManager data){
    	try {
    		service.save(data);
    		return Message.success("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
			return Message.error(1,"保存失败");
		}

	}


	@ApiOperation(value = "证章管理删除", notes = "通过id删除证章管理信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
    //@PreAuthorize("hasAuthority('tb:badges:manager:delete')")
	public Message delete(@RequestParam(value="id") String id) {
		try {
			service.removeById(id);
			return Message.success("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
			return Message.error(1,"删除成功");
		}

	}

	@ApiOperation(value = "批量删除", notes = "批量删除。")
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	public Message batchDelete(@RequestBody List<String> list) {
		try {
			service.removeByIds(list);
			return Message.success("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
			return Message.error(1,"删除成功");
		}
    }

	@ApiOperation(value = "分页查询证章管理list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findList",method = RequestMethod.POST)
    //@PreAuthorize("hasAuthority('tb:badges:manager:view')")
	public Message findList(@RequestBody Map<String,Object> data) {
        try {
        	IPage<TbBadgesManager> selectPage = service.selectPage(data);
			return Message.success("保存成功",selectPage.getRecords(),selectPage.getTotal());
        } catch (Exception e) {
        	e.printStackTrace();
			return Message.error(1,"保存成功");
        }
	}

	@ApiOperation(value = "查询证章管理所有数据list", notes = "查询证章管理所有数据list。")
	@RequestMapping(value="/findAll",method = RequestMethod.POST)
    //@PreAuthorize("hasAuthority('tb:badges:manager:view')")
	public Message findAll(){
        return Message.success("保存成功",service.selectAll());
	}


//	@ApiOperation(value = "通过id获取证章管理实体类", notes = "通过id获取证章管理实体类。")
//	@RequestMapping(value="/getById",method = RequestMethod.GET)
//    //@PreAuthorize("hasAuthority('tb:badges:manager:view')")
//	public Result getById(@RequestParam(value="id") String id) {
//        return new Result<>(Result.CODE_SUCCESS,"",service.getById(id));
//	}
//
//	@ApiOperation(value = "通过id获取实体类详细信息", notes = "通过id获取实体类详细信息。")
//	@RequestMapping(value="/detail",method = RequestMethod.GET)
//    //@PreAuthorize("hasAuthority('tb:badges:manager:view')")
//	public Result detail(@RequestParam(value="id") String id) {
//        return new Result<>(Result.CODE_SUCCESS,"",service.getById(id));
//	}

	@ApiOperation(value = "根据类别获取证照/章名称", notes = "根据类别获取证照/章名称")
	@RequestMapping(value="/getNameList",method = RequestMethod.POST)
	public Message getNameList() {
		try {

			QueryWrapper<TbBadgesManager> queryWrapper = new QueryWrapper<>();
			queryWrapper.select("id","badges_name","type");
			queryWrapper.eq("del_flag",0);
			List<TbBadgesManager> list = service.list(queryWrapper);

			return Message.success("成功",list);
		}catch (Exception e){
			e.printStackTrace();
			return Message.error(1,"失败");
		}

	}

	@ApiOperation(value = "获取所有的部门信息", notes = "获取所有的部门信息")
	@RequestMapping(value="/getAllDept",method = RequestMethod.POST)
	//@PreAuthorize("hasAuthority('tb:badges:manager:view')")
	public Message getAllDept() {
		List<Map<String, Object>> allDept = service.getAllDept();
		return Message.success("成功",allDept);
	}


	@ApiOperation(value = "获取证章管理列表", notes = "获取证章管理列表")
	@RequestMapping(value = "/getBadgesManagerList", method = RequestMethod.GET)
	public Message getBadgesManagerList(){
		try {
			return Message.success("保存成功" ,service.badgesManagerList(1));
		} catch (Exception e) {
			log.error("保存失败:{}",e);
			// TODO: handle exception
			return Message.error("保存失败");
		}
	}

}

